//Main PhotoZoom Function
function PhotoZoom_Init()
{
  PhotoZoom_AddContentEvents();
  PhotoZoom_AddCssStyle();
}

function PhotoZoom_AddCssStyle()
{
  $("head").append('<style type="text/css">\n' +
  '#PhotoZoom { position:absolute; min-height:50px; min-width:50px; border:1px solid #000; background:' + getValue('photozoom_bg_color', '#FFF3E4') + '; padding:5px; display:none; color:#000;  z-index: 5006; }\n' +
  '#PhotoZoom img{ min-height:50px; min-width:50px; background: url("' + chrome.extension.getURL("images/loading.gif") + '") no-repeat center; }\n' +
  '#PhotoZoom p{ padding-top: 3px; margin-bottom:0px; }\n' +
  '</style>');
}

//PhotoZoom - Zommed Image
function PhotoZoom_IsZoomable(src)
{
  return (PhotoZoom_GetFullImg(src) != null);
}

function PhotoZoom_GetFullImg(src)
{
  if(/_full,r,120x90/.test(src))
    return src.replace('_full,r,120x90', '_full,r,470x470');
	
  else if(/upt/.test(src))
    return src.replace('/upt/', '/upf/');
  else if(/up1/.test(src))
    return src.replace('/up1/', '/upf/');
  
  else if(/u200/.test(src))
    return src.replace('/u200/', '/u400/');
  else if(/u100/.test(src))
    return src.replace('/u100/', '/u400/');
  else if(/u50/.test(src))
    return src.replace('/u50/', '/u400/');
    
  else if(/u1/.test(src))
    return src.replace('/u1/', '/u4/');
  else if(/u2/.test(src))
    return src.replace('/u2/', '/u4/');
  else if(/u3/.test(src))
    return src.replace('/u3/', '/u4/');
  
  else if(/nopic_100/.test(src))
    return src.replace('nopic_100', 'nopic_200');
  else if(/nopic_50/.test(src))
    return src.replace('nopic_50', 'nopic_200');
  
  else if(/lockpic_100/.test(src))
    return src.replace('lockpic_100', 'lockpic_200');
  else if(/lockpic_50/.test(src))
    return src.replace('lockpic_50', 'lockpic_200');
  
  else if(/small/.test(src))
    return src.replace('/small/', '/big/');
  else if(/square-85/.test(src))
    return src.replace('square-85', 'square-300');
	
  else if(/i.kw.cx/.test(src) && /upf/.test(src))
    return src;
  
  else
    return null;
}

//PhotoZoom - Content Events
function PhotoZoom_AddContentEvents()
{
  document.addEventListener ('DOMNodeInserted', PhotoZoom_OnNodeAdded, false);
  document.onmousemove = function(e)
  {
    PhotoZoom_OnMouseMove(e);
  };
}

function PhotoZoom_OnNodeAdded()
{
  var i;
  
  var imgs = this.getElementsByTagName('img');
  for(i = 0; i < imgs.length; i++)
  {
    PhotoZoom_AddImageEvents(imgs[i]);
  }
  
  var anchors = this.getElementsByTagName('a');
  for(i = 0; i < anchors.length; i++)
  {
    if(/url/.test(anchors[i].style.backgroundImage))
        PhotoZoom_AddAnchorsEvents(anchors[i]);
  }
}

function PhotoZoom_AddImageEvents(Img)
{
  if(PhotoZoom_IsZoomable(Img.src))
  {
    //add the function to the image
    Img.onmouseover = function(e)
    {
      PhotoZoom_OnMouseOver(this, e);
    };
    Img.onmouseout = function()
    {
      PhotoZoom_OnMouseout(this);
    };
    //MiniProfil deaktivieren
    if(/canvas/.test(Img.className) || /thumb/.test(Img.className))
    {
      Img.parentNode.onmouseover = null;
    }
  }
}
function PhotoZoom_AddAnchorsEvents(anchor)
{
  var src = anchor.style.backgroundImage;
  src = src.substring(src.indexOf('(')+1, src.lastIndexOf(')')-1);
  
  if(PhotoZoom_IsZoomable(src))
  {
    //add the function to the image
    anchor.onmouseover = function(e)
    {
      PhotoZoom_OnMouseOver(this, e);
    };
    anchor.onmouseout = function()
    {
      PhotoZoom_OnMouseout(this);
    };
    
    /*
    //MiniProfil deaktivieren
    if(/canvas/.test(anchor.className) || /thumb/.test(anchor.className))
    {
      anchor.parentNode.onmouseover = null;
    }*/
  }
}

//PhotoZoom - Eventhandler
var PhotoZoom_Title = null;
var PhotoZoom_ParentTitle = null;
var PhotoZoom_MouseEvent = null;
function PhotoZoom_OnMouseOver(img, e)
{
  //Mouse Pos speichern
  PhotoZoom_MouseEvent = e;
  
  //Image Url
  var src;
  if(img.tagName == "a" || img.tagName == "A")
  {
      src = img.style.backgroundImage;
      src = src.substring(src.indexOf('(')+1, src.lastIndexOf(')')-1);
  }
  else
      src = img.src;

  //Title
  PhotoZoom_ParentTitle = img.parentNode.title;
  PhotoZoom_Title = img.title;
  img.title = "";
  img.parentNode.title = "";

  //Title setzen
  var title = "";

  if (PhotoZoom_ParentTitle)
    title = PhotoZoom_ParentTitle;
  else if(PhotoZoom_Title)
    title = PhotoZoom_Title;
  else if (img.parentNode.alt)
    title = img.parentNode.alt;
  else if (img.alt)
    title = img.alt;

  if(title)
    title = "<p style='display: none;' id='PhotoZoom_Text'>" + title + "</p>";

  //Tooltip anhängen
  $("body").append("<div id='PhotoZoom'></div>");
  $("#PhotoZoom").append("<img id='PhotoZoom_Load' style='display: ;' />");

  //Bild anhängen
  var PhotoZoom_Img = new Image();
  PhotoZoom_Img.id = "PhotoZoom_Img";
  $(PhotoZoom_Img).css("display", "none");
  PhotoZoom_Img.alt = "Image preview";
  PhotoZoom_Img.src = PhotoZoom_GetFullImg(src);
  $(PhotoZoom_Img).load( function(e)
  {
    $(this).css("display", "");
    $(document.getElementById("PhotoZoom_Text")).css("display", "");
    $(document.getElementById("PhotoZoom_Load")).css("display", "none");
    PhotoZoom_setTooltipPosition(PhotoZoom_MouseEvent);
  });
  $("#PhotoZoom").append(PhotoZoom_Img);

  //Bild-titel
  $("#PhotoZoom").append(title);

  //Tooltip anpassen
  $("#PhotoZoom")
  .css("top", PhotoZoom_getTooltipPosition(e).y + "px")
  .css("left", PhotoZoom_getTooltipPosition(e).x + "px")
  .css("position", "absolute")
  .fadeIn("fast");
}

function PhotoZoom_OnMouseout(img)
{
  img.title = PhotoZoom_Title;
  img.parentNode.title = PhotoZoom_ParentTitle;

  $("#PhotoZoom").remove();
}

function PhotoZoom_OnMouseMove(e)
{
  PhotoZoom_MouseEvent = e;
  PhotoZoom_setTooltipPosition(e);
}

//PhotoZoom - Tooltip Funktionen
function PhotoZoom_setTooltipPosition(e)
{
  $("#PhotoZoom")
  .css("top", PhotoZoom_getTooltipPosition(e).y + "px")
  .css("left", PhotoZoom_getTooltipPosition(e).x + "px");
}

function PhotoZoom_getTooltipPosition(e)
{
  var offset_x = 10;
  var x = e.pageX + offset_x;
  var y = e.pageY - getElementSize('PhotoZoom').h / 2;
  var win_x = window.innerWidth + window.pageXOffset;
  var win_y = window.innerHeight + window.pageYOffset;

  //Rechts ausserhalb des sichtbaren Bereiches
  if(x + getElementSize('PhotoZoom').w > win_x)
  {
    //Links mehr Platz verfügbar?
    if(x + getElementSize('PhotoZoom').w - win_x > getElementSize('PhotoZoom').w + offset_x - e.pageX )
      x = e.pageX - offset_x - getElementSize('PhotoZoom').w;
  }

  //Unten ausserhalb des sichtbaren Bereiches
  if((y + getElementSize('PhotoZoom').h) > win_y)
    y -= y + getElementSize('PhotoZoom').h - win_y;
  //Oben ausserhalb des sichtbaren Bereiches
  else if(y < window.pageYOffset)
    y = window.pageYOffset;

  return {x : x, y : y};
}